//
//  YSCommentView.m
//  Yueshi
//
//  Created by yueshi－mac1 on 15/12/9.
//  Copyright © 2015年 悦食中国. All rights reserved.
//

#import "YSCommentView.h"

@interface YSCommentView()
{
    UIImageView *_iconImageView;
    UILabel *_numLabel;
}

@end

@implementation YSCommentView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _numFont = 8;
        _overtopImage = 2;
        _leftPadding = 3;
        _topPadding = 1;
        
        _iconImageView = [[UIImageView alloc] init];
        [self addSubview:_iconImageView];
        
        _numLabel = [[UILabel alloc]init];
        [_numLabel setTextColor:[UIColor whiteColor]];
        [_numLabel setFont:[UIFont systemFontOfSize:_numFont]];
        [_numLabel setTextAlignment:NSTextAlignmentCenter];
        [_numLabel setHidden:YES];
        
        _numLabel.layer.masksToBounds = YES;
        [_numLabel.layer setCornerRadius:0];
        [_numLabel.layer setBorderColor:RGBColor(204, 204, 204).CGColor];
        [_numLabel.layer setBackgroundColor:[ThemeColor CGColor]];
        
        [self addSubview:_numLabel];
    }
    return self;
}

- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    
    [self setIconImage:_iconImageView.image];
    
}

- (void)setIconImage:( UIImage *)image{
    if (!image) return;
    
    if (!self.size.width) {
        _iconImageView.frame = CGRectMake(0, _overtopImage, image.size.width, image.size.height);
        
        [self setBounds:CGRectMake(0, 0, image.size.width, image.size.height + _overtopImage)];
    }else{
        _iconImageView.frame = CGRectMake(self.size.width/2 - image.size.width/2, self.size.height/2 - image.size.height/2, image.size.width, image.size.height);
    }
    
    _iconImageView.image = image;
    [self setNum:_num];
}

- (void)setNum:(NSInteger)num{
    if (num <0) {
        num = 0;
    }
    _num = num;
    
    if(!num || _iconImageView.size.width == 0){
        [_numLabel setHidden:YES];
        return;
    }
    
    NSString *numStr = [NSString stringWithFormat:@"%lu",(unsigned long)num];
    
    [_numLabel setHidden:NO];
    if(num>999){
        numStr = @"999+";
    }
    
    CGSize size = [numStr sizeWithFont:SYS_FONT(_numFont)];
    CGFloat numWidth = size.width +_leftPadding*2;
    CGFloat numHeight = size.height + _topPadding *2;
    
    if (num<10) {
        numWidth = numHeight;
    }
    
    [_numLabel.layer setCornerRadius:numHeight*0.5];
    
    [_numLabel setFrame:CGRectMake(_iconImageView.image.size.width-(numWidth)*0.5, _iconImageView.frame.origin.y - _overtopImage, numWidth, numHeight)];
    
    if (self.size.width == _iconImageView.size.width) {
        CGRect bounds  = self.bounds;
        bounds.size.width = bounds.size.width + numWidth *0.5;
        self.bounds = bounds;
    }
    
    [_numLabel setText:numStr];
}

- (void)setOvertopImage:(CGFloat)overtopImage{
    _overtopImage = overtopImage;
    [self setNum:_num];
}

- (void)setLeftPadding:(CGFloat)leftPadding{
    _leftPadding = leftPadding;
    [self setNum:_num];
}

- (void)setTopPadding:(CGFloat)topPadding{
    _topPadding = topPadding;
    [self setNum:_num];
}

- (void)setNumFont:(CGFloat)numFont{
    _numFont = numFont;
    [self setNum:_num];
}

- (void)setNumBackgroundColor:(UIColor *)numBackgroundColor{
    _numLabel.backgroundColor = numBackgroundColor;
}

- (void)setNumColor:(UIColor *)numColor{
    _numLabel.textColor = numColor;
}



@end
